package com.cms.controller.admin;

import com.alibaba.fastjson.JSONArray;
import com.cms.domain.Feedback;
import com.cms.entity.Role;
import com.cms.entity.RoleSite;
import com.cms.service.MenuService;
import com.cms.service.RoleService;
import com.cms.service.RoleSiteService;
import com.cms.service.SiteService;
import org.apache.commons.lang.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;


/**
 * Controller - 角色
 * 
 * 
 * 
 */
@Controller("adminRoleController")
@RequestMapping("/admin/role")
public class RoleController extends BaseController {

	@Autowired
	private RoleService roleService;
	@Autowired
	private SiteService siteService;
	@Autowired
	private MenuService menuService;
	@Autowired
	private RoleSiteService roleSiteService;

	/**
	 * 列表
	 */
	@RequestMapping
	public String index(Integer pageNumber, ModelMap modelMap) {
		setListQuery();
		if(pageNumber==null){
			pageNumber = 1;
		}
		modelMap.addAttribute("page", roleService.findPage(pageNumber,PAGE_SIZE));
		return getView("role/index");
	}

	/**
	 * 添加
	 */
	@RequestMapping("/add")
	public String add(ModelMap modelMap) {
		modelMap.addAttribute("sites", siteService.findAll());
		modelMap.addAttribute("rootMenus", menuService.findRoots());
		return getView("role/add");
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public String save(Role role,String[] permissions,Integer[] siteIds) {
        if(ArrayUtils.isNotEmpty(permissions)){
        	role.setPermission(JSONArray.toJSONString(permissions));
        }else{
        	role.setPermission("[]");
        }
        role.setCreateDate(LocalDateTime.now());
        role.setUpdateDate(LocalDateTime.now());
        roleService.save(role);
        for(Integer siteId : siteIds){
        	RoleSite roleSite = new RoleSite();
        	roleSite.setRoleId(role.getId());
        	roleSite.setSiteId(siteId);
			roleSiteService.save(roleSite);
        }
		return "redirect:"+getListQuery("/admin/role");
	}

	/**
	 * 编辑
	 */
	@RequestMapping("/edit")
	public String edit(Integer id,ModelMap modelMap) {
		modelMap.addAttribute("role", roleService.getById(id));
		modelMap.addAttribute("rootMenus", menuService.findRoots());
		modelMap.addAttribute("sites", siteService.findAll());
		return getView("role/edit");
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public String update(Role role,String[] permissions,Integer[] siteIds) {
        if(ArrayUtils.isNotEmpty(permissions)){
        	role.setPermission(JSONArray.toJSONString(permissions));
        }else{
        	role.setPermission("[]");
        }
        role.setUpdateDate(LocalDateTime.now());
        roleService.updateById(role);
        roleSiteService.deleteByRoleId(role.getId());
        for(Integer siteId : siteIds){
        	RoleSite roleSite = new RoleSite();
        	roleSite.setRoleId(role.getId());
        	roleSite.setSiteId(siteId);
			roleSiteService.save(roleSite);
        }
		return "redirect:"+getListQuery("/admin/role");
	}

	/**
	 * 删除
	 */
	@ResponseBody
	@RequestMapping("/delete")
	public Feedback delete(Integer[] ids) {
		if(ArrayUtils.isNotEmpty(ids)){
			roleService.removeByIds(Arrays.asList(ids));
		}
		return Feedback.success(new HashMap<>());
	}

}